package com.gagakuai.controller;


import com.gagakuai.annotation.Log;
import com.gagakuai.base.CommonResultDTO;
import com.gagakuai.domain.dto.LogQuery;
import com.gagakuai.service.LogService;
import com.gagakuai.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/ca/logs")
@Api(tags = "系统：日志管理")
public class LogController {

    private final LogService logService;

    public LogController(LogService logService) {
        this.logService = logService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, LogQuery criteria) throws IOException {
        criteria.setLogType("INFO");
        logService.download(logService.queryAll(criteria), response);
    }

    @Log("导出错误数据")
    @ApiOperation("导出错误数据")
    @GetMapping(value = "/error/download")
    public void downloadErrorLog(HttpServletResponse response, LogQuery criteria) throws IOException {
        criteria.setLogType("ERROR");
        logService.download(logService.queryAll(criteria), response);
    }
    @GetMapping
    @ApiOperation("日志查询")
    public ResponseEntity<Object> query(LogQuery criteria, Pageable pageable){
        pageable = PageRequest.of(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
        criteria.setLogType("INFO");
        return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/getAllLog")
    @ApiOperation("用户日志查询")
    public CommonResultDTO<Object> queryUserLog(LogQuery criteria, Pageable pageable){
        pageable = PageRequest.of(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
        criteria.setBlurry(SecurityUtils.getCurrentUsername());
        Object o=logService.queryAllByUser(criteria,pageable);
        return new CommonResultDTO(o);
    }

    @GetMapping(value = "/error")
    @ApiOperation("错误日志查询")
    public ResponseEntity<Object> queryErrorLog(LogQuery criteria, Pageable pageable){
        pageable = PageRequest.of(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
        criteria.setLogType("ERROR");
        return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/error/{id}")
    @ApiOperation("日志异常详情查询")
    public ResponseEntity<Object> queryErrorLogs(@PathVariable Long id){
        return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK);
    }
    @DeleteMapping(value = "/del/error")
    @Log("删除所有ERROR日志")
    @ApiOperation("删除所有ERROR日志")
    public ResponseEntity<Object> delAllErrorLog(){
        logService.delAllByError();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping(value = "/del/info")
    @Log("删除所有INFO日志")
    @ApiOperation("删除所有INFO日志")
    public ResponseEntity<Object> delAllInfoLog(){
        logService.delAllByInfo();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
